<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="clickAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" align="center" prop="name" />
          <el-table-column label="标识" align="center" prop="code" />
          <el-table-column label="描述" align="center" prop="description" />
          <el-table-column label="操作" align="center">
            <template #default="{row}">
              <el-button v-if="row.type===1" size="small" type="text" @click="clickAdd(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="editPermission(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 新增/编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="title" @close="btnCancel">
        <!-- 表单内容 -->
        <el-form ref="permForm" label-width="100px" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="btnCancel">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permisson' // 获取所有权限点列表  增加权限  删除权限  回显  更新
import { tranListTotreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      list: [], // 所有的权限点列表
      showDialog: false, // 弹窗
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 权限名称
        code: '', // 权限标识,用于将来判断
        description: '', // 权限描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() { // 转成树形结构
      const res = await getPermissionList()
      this.list = tranListTotreeData(res, '0')
    },
    clickAdd(type, pid) { // 点击添加权限触发
    // 访问权type是1 按钮操作权是二
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    async clickSubmit() { // 点击确定
      await this.$refs.permForm.validate() // 兜底校验
      // 如果有id就是编辑权限点
      if (this.formData.id) {
        await updatePermission(this.formData)
        this.$message.success('修改权限点成功')
      } else { // 新增权限接口
        await addPermission(this.formData)
        this.$message.success('增加权限成功')
      }
      this.getPermissionList()
      this.btnCancel()
    },
    delPermission(id) { // 点击删除该权限
      this.$confirm('此操作将永久删除权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.$message({
          type: 'success',
          message: '删除权限成功!'
        })
        await delPermission(id)
        this.getPermissionList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async editPermission(id) { // 查看回显
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    btnCancel() { // 弹窗关闭清空数据
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 权限名称
        code: '', // 权限标识,用于将来判断
        description: '', // 权限描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
      this.$refs.permForm.resetFields() // 移除校验
      this.showDialog = false
    }

  }
}
</script>
